package com.yh.javabean;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.util.List;
public class mappera {


    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration ( value = "classpath:spring-mybatis.xml" )
    public class AppConfigTest {

        private UserMapper userMapper;
        private DataSource dataSource;

        @Before
        public void setUp() throws Exception {
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
            userMapper = context.getBean(UserMapper.class);
            dataSource = context.getBean(DataSource.class);

            // Initialize database
            try (Connection conn = dataSource.getConnection();
                 Statement stmt = conn.createStatement()) {
                stmt.execute("CREATE TABLE IF NOT EXISTS user (id INT PRIMARY KEY, name VARCHAR(100))");
                stmt.execute("INSERT INTO user (id, name) VALUES (1, 'John Doe')");
            }
        }

        public UserMapper getUserMapper() {
            return userMapper;
        }

        public void testFindAll() {
            List<user.User> users = userMapper.findAll();
            for (com.yh.javabean.user.User user : users) {
                System.out.println(user.getName());
            }
        }
    }

}
